//
//  LGBottonView.m
//  lgdw
//
//  Created by Apple on 15/12/9.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "LGBottonView.h"
#import "LGDiscover.h"

#define Margen 10
@interface LGBottonView ()
///  发布者图
@property (nonatomic, strong) UIImageView *avatarView;
///  发布者姓名
@property (nonatomic, strong) UILabel *namaLabel;
///  点赞数
@property (nonatomic, strong) UIButton *like_numBtn;
///  评论数
@property (nonatomic, strong) UIButton *comment_num;
///  线
@property (nonatomic, strong) UILabel *lineView;
///  线
@property (nonatomic, strong) UILabel *lineView1;


@end

@implementation LGBottonView
#pragma mark - /************** 设置数据 **************/
- (void)setDiscover:(LGDiscover *)discover {
    
    // 防止重用
    self.avatarView.image = nil;
    self.namaLabel.text = nil;
    [self.comment_num setTitle:nil forState:UIControlStateNormal];
    [self.like_numBtn setTitle:nil forState:UIControlStateNormal];
    
    _discover = discover;
    // 头像
    [self.avatarView sd_setImageWithURL:[NSURL URLWithString:discover.avatar] placeholderImage:[UIImage imageNamed:@"icon_80"]];
//    [self.avatarView sd_setImageWithURL:[NSURL URLWithString:discover.avatar]];
    self.namaLabel.text = discover.name;

    // 赞
    [self.like_numBtn setTitle:[NSString stringWithFormat:@"  %@",discover.like_num] forState:UIControlStateNormal];
    [self.like_numBtn addTarget:self action:@selector(like_numBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    // 评论
    [self.comment_num setTitle:[NSString stringWithFormat:@"  %@",discover.comment_num] forState:UIControlStateNormal];
    [self.comment_num addTarget:self action:@selector(comment_numClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - /************** 按钮点击方法 **************/
/** 点击赞  */
- (void)like_numBtnClick:(UIButton *)btn {
    btn.selected = !btn.selected;
    if (!btn.selected) {
        [self.like_numBtn setTitle:[NSString stringWithFormat:@"  %@",self.discover.like_num] forState:UIControlStateNormal];
        return;
    }
    [btn setTitle:[NSString stringWithFormat:@"  %d",[self.discover.like_num intValue] + 1] forState:UIControlStateNormal];

}
/** 点击评论  */
- (void)comment_numClick:(UIButton *)btn {
    
    NSLog(@"点击发现页面的评论");
    
}
 #pragma mark - /************** 构造方法 **************/
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - /************** 设置界面 **************/
- (void)setupUI {
    
    // 加载控件
    [self addSubview:self.avatarView];
    [self addSubview:self.namaLabel];
    [self addSubview:self.like_numBtn];
    [self addSubview:self.comment_num];
    [self addSubview:self.lineView];
    [self addSubview:self.lineView1];
    
    // 设置布局
    // 头像
    [self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(Margen);
        make.width.mas_equalTo(30);
        make.top.mas_equalTo(4);
        make.height.mas_equalTo(30);
        
    }];
    
    // 名字
    [self.namaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.avatarView.mas_right).offset(Margen);
        make.centerY.mas_equalTo(self.avatarView.mas_centerY);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-Margen);
    }];
  
    
    // 评论
    [self.comment_num mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(- 2 * Margen);
        make.centerY.mas_equalTo(self.mas_centerY);
        
    }];
    // 线1
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.comment_num.mas_left).offset(- 2 * Margen);
        make.top.mas_equalTo(self.mas_top).offset(Margen);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-Margen);
        make.width.mas_equalTo(1);
        
    }];
    
    // 点赞
    [self.like_numBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.lineView.mas_left).offset(- 2 * Margen);
        make.centerY.mas_equalTo(self.mas_centerY);
        
    }];
    // 线2
    [self.lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.like_numBtn.mas_left).offset(- 2 * Margen);
        make.top.mas_equalTo(self.mas_top).offset(Margen);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-Margen);
        make.width.mas_equalTo(1);
    }];
}

#pragma mark - /************** 懒加载控件 **************/
// 头像
- (UIImageView *)avatarView {
    if (_avatarView == nil) {
        _avatarView = [[UIImageView alloc] initAndClipsToBounds];
        // 圆形图片
        _avatarView.layer.cornerRadius = 15;
        _avatarView.layer.borderWidth = 1;
        _avatarView.layer.borderColor = [[UIColor alloc] initWithWhite:0.5 alpha:0.5].CGColor;
        _avatarView.layer.masksToBounds = YES;
        [_avatarView setImage:[UIImage imageNamed:@"icon_80"]];
    }
    return _avatarView;
}
// name
- (UILabel *)namaLabel {
    if (_namaLabel == nil) {
        _namaLabel = [[UILabel alloc] initWithTitle:@"aaa" FontSize:12 TextColor:nil];
        
    }
    return _namaLabel;
}


// 评论
- (UIButton *)comment_num {
    if (_comment_num == nil) {
        _comment_num = [[UIButton alloc] initWithTitle:@"  -1" ImageName:nil];
        
        
        [_comment_num setImage:[UIImage imageNamed:@"shai_comment"] forState:UIControlStateNormal];
            }
    return _comment_num;
}

// 线1
- (UILabel *)lineView {
    if (_lineView == nil) {
        _lineView = [[UILabel alloc] init];
        _lineView.backgroundColor = [[UIColor alloc] initWithWhite:0.7 alpha:0.5];
    }
    return _lineView;
}

// 赞
- (UIButton *)like_numBtn {
    if (_like_numBtn == nil) {
        _like_numBtn = [[UIButton alloc] initWithTitle:@"  -1" ImageName:@"detail_like"];
        
    }
    
    return _like_numBtn;
}
// 线1
- (UILabel *)lineView1 {
    if (_lineView1 == nil) {
        _lineView1 = [[UILabel alloc] init];
        _lineView1.backgroundColor = [[UIColor alloc] initWithWhite:0.7 alpha:0.5];
    }
    return _lineView1;
}

@end
